﻿Public Class CINNERLabelState2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState2.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-IN") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
        Debug.WriteLine("CINNERLabelState2.DoExecute")
        Dim readText As String = task.ProcessText
        Dim itemCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_IN_ITEM_CD)
        Dim uniqueCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_IN_UNIQUE_CD)
        Dim inBoxQty As Integer = CCommon.From36To10(CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_IN_QTY))
        Dim isFraction As Boolean


        ''読込み済みのラベルではないか確認（ユニークコード）N:アラート
        Dim uniqueCdInfo As CPDATask2.strctUniqueCdInfo = Nothing
        uniqueCdInfo.labelType = CCommon.LABEL_INNER
        uniqueCdInfo.itemCd = itemCd
        uniqueCdInfo.uniqueCd = uniqueCd
        uniqueCdInfo.readDateTime = Now()
        ''uniqueCdInfo.orderCd = ""

        Select Case pdaTask.addUniqueCd(uniqueCdInfo)
            Case -1
                ''アラート
                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        pdaTask.Hardware.alert()
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_015, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        'Debug.WriteLine(CCommon.CNS_MSG_015)

                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

            Case 2
                ''マツダ重複
            Case 1
                ''区切りラベル
                ''（親ラベルが存在しているかのチェック）
                If pdaTask.isReadParentLabel() Then
                    ''前回のシングル構造体をLabelInfoにセット
                    ''If pdaTask.addLabelInfo() Then
                    ''新規でシングル構造体を作成
                    ''pdaTask.clrSingleLabelInfo()
                    ''End If
                    If inBoxQty = 0 Then
                        ''入数が0の場合は端数扱い　入力を求める
                        isFraction = True
                        Dim prevWindowMode As EnumWindowMode
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        Do While inBoxQty <= 0
                            Dim result As String = ""
                            Do While Not IsNumeric(result)
                                If result = "" Then

                                    pdaTask.Hardware.alert()
                                    pdaTask.WindowManager.SetInputMessage(CCommon.CNS_MSG_022, CCommon.COLOR_BLACK, CCommon.COLOR_GRAY)
                                    pdaTask.WindowManager.SetInputValue(pdaTask.getStrDifQty(), CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.INPUT_MODE)
                                    'Debug.WriteLine(CCommon.CNS_MSG_022)

                                End If
                                result = task.ReadTextWait(-1)
                            Loop
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                            inBoxQty = CInt(result)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                    End If

                    If pdaTask.addSingleLabelInfo(readText, "", itemCd, CCommon.LABEL_INNER, inBoxQty, isFraction) Then
                        ''正常な場合、数量が一致したときにアラームを鳴らす
                        If pdaTask.checkEnd() Then
                            pdaTask.Hardware.nextB()
                        End If
                    Else

                        ''エラー
                        pdaTask.Hardware.warning()
                        ''対象ではないラベル
                        pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            End If
                            result = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)




                        task.ErrorCd = "ER"
                        task.ProcessText = ""
                        nextState = GetNextState(task, GetType(CErrorState2))
                        Return nextState
                    End If

                Else
                    ''エラー
                    pdaTask.Hardware.warning()
                    ''対応する親ラベルが読まれていない
                    pdaTask.delLastUniqueCd() ''登録したユニークコードを削除


                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)




                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState2))
                    Return nextState
                End If


        End Select

        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState2.DoExit")
        Return True
    End Function

End Class
